//
//  Target_A.m
//  New_Runway
//
//  Created by mack on 2018/1/27.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "Target_A.h"
#import "NSObject+Target.h"


#import "CarOrderVC.h"
#import "SaleOutLibaryVC.h"
#import "SaleSetPriceVC.h"
#import "EditCarInfoVC.h"
#import "CarEspeciallybuyVC.h"
#import "CarBaseInfoModel.h"

@interface Target_A ()
@property (nonatomic, strong) CarBaseInfoModel *infomodel;
@property (nonatomic, strong) NSIndexPath *indexPath;
@end

@implementation Target_A


- (void)Action_carset:(NSDictionary *)params
{
    self.infomodel = params[@"params"][@"info"];
    self.indexPath = params[@"params"][@"index"];

      _callback = params[@"callback" ];
    
    if ([ params[@"params"][@"vc"] isEqualToString:@"车辆预定"]) {
        
        if (self.infomodel.result.carOrderState == 1) {
            //已预订
            [self aleartViewShow:@"是否取消预定" tag:5];
        }else{
            CarOrderVC *orderVC = [[CarOrderVC alloc] init];
            orderVC.parems = params[@"params"];
            orderVC.indexPath = self.indexPath;
            orderVC.shopID = self.infomodel.result.shopId;
            [[self getTopVC] cyl_pushViewController:orderVC animated:YES];

        }

    }else if ([ params[@"params"][@"vc"] isEqualToString:@"销售出库"]){
        SaleOutLibaryVC *outVc = [[SaleOutLibaryVC alloc] init];
        outVc.parems = params[@"params"];
        outVc.shopID = self.infomodel.result.shopId;
        outVc.selIndex = params[@"params"][@"index"];
        [[self getTopVC] cyl_pushViewController:outVc animated:YES];
//        [self aleartViewShow:@"是否销售出库" tag:7];

    }else if ([ params[@"params"][@"vc"] isEqualToString:@"销售定价"]){
        SaleSetPriceVC *setPriceVc = [[SaleSetPriceVC alloc] init];
        setPriceVc.parems = params[@"params"];
        setPriceVc.indexPath = self.indexPath;
        [[self getTopVC] cyl_pushViewController:setPriceVc animated:YES];
        
    }else if ([ params[@"params"][@"vc"] isEqualToString:@"编辑车辆"]){

        
        EditCarInfoVC *editVc = [[EditCarInfoVC alloc] init];
        editVc.infomodel = params[@"params"][@"info"];
        editVc.titleL = @"编辑车辆";
        editVc.selIndex = self.indexPath;
        editVc.isReject = [params[@"params"][@"reject"] integerValue];
        [[self getTopVC] cyl_pushViewController:editVc animated:YES];
        
    }else if ([ params[@"params"][@"vc"] isEqualToString:@"车辆特卖"]){
        CarEspeciallybuyVC *buyVC = [[CarEspeciallybuyVC alloc] init];
        buyVC.parems = params[@"params"];
        buyVC.indexPath = self.indexPath;
        [[self getTopVC] cyl_pushViewController:buyVC animated:YES];
        
    }else if ([ params[@"params"][@"vc"] isEqualToString:@"申请下架"]){
     
        
        [self aleartViewShow:@"是否申请下架" tag:1];
        
    }else if ([ params[@"params"][@"vc"] isEqualToString:@"申请上架"]){
        
        
        [self aleartViewShow:@"是否申请上架" tag:3];


    }else if ([ params[@"params"][@"vc"] isEqualToString:@"申请入库"]){
     

        EditCarInfoVC *editVc = [[EditCarInfoVC alloc] init];
        editVc.selIndex = params[@"params"][@"index"];
        editVc.infomodel = params[@"params"][@"info"];
        editVc.titleL = @"申请入库";
        [[self getTopVC] cyl_pushViewController:editVc animated:YES];
        
    }else if ([ params[@"params"][@"vc"] isEqualToString:@"申请复检"]){
        [self aleartViewShow:@"是否申请复检" tag:4];
        
    }else if ([params[@"params"][@"vc"] isEqualToString:@"申请临时出场"]){
        [self aleartViewShow:@"是否申请临时出场" tag:2];
        
    }else if ([params[@"params"][@"vc"] isEqualToString:@"撤销入库"]){
        [self aleartViewShow:@"是否撤销入库申请" tag:6];

    }
    
   
    
}
-(void)aleartViewShow:(NSString *)title tag:(NSInteger)tag{
    
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    [alertVc addAction:cancelAction];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self dataPost:tag];
        
        
    }];
    [alertVc addAction:okAction];
    
    [[self getTopVC] presentViewController:alertVc animated:YES completion:^{
        
        
        
    }];
}

-(void)dataPost:(NSInteger)tag{
    NSDictionary *dic;
    if (tag == 1) {//申请下架
      
        dic = @{
                @"carId":self.infomodel.result.carId,
                @"stateKey":@"carSaleState",
                @"stateValue":@30,
                @"userId":[NRWCarUser sharedUser].userId
                
                };
        
    }else if (tag == 2){//申请临时出场
        
        dic = @{
                @"carId":self.infomodel.result.carId,
                @"stateKey":@"carSiteState",
                @"stateValue":@420,
                @"userId":[NRWCarUser sharedUser].userId

                };

    }else if (tag == 3){//申请上架
        
        dic = @{
                @"carId":self.infomodel.result.carId,
                @"stateKey":@"carSaleState",
                @"stateValue":@20,
                @"userId":[NRWCarUser sharedUser].userId
                
                };
        
    }else if (tag == 4){//申请检测
        
        dic = @{
                @"carId":self.infomodel.result.carId,
                @"stateKey":@"carDetectState",
                @"stateValue":@530,
                @"userId":[NRWCarUser sharedUser].userId,
                @"marketId":[NRWCarUser sharedUser].marketId
                    
                };
    }else if (tag == 5){//车辆预定
        
        dic = @{
                @"carId":self.infomodel.result.carId,
                @"stateKey":@"carOrderState",
                @"stateValue":@0,
                @"userId":[NRWCarUser sharedUser].userId
                
                };
    }if (tag == 6){//撤销入库
        
        dic = @{
                @"carId":self.infomodel.result.carId,
                @"userId":[NRWCarUser sharedUser].userId
                };
        
    }else if (tag == 7){//销售出库
        dic = @{
                @"carId":self.infomodel.result.carId,
                @"stateKey":@"carStockState",
                @"stateValue":@40,
                @"userId":[NRWCarUser sharedUser].userId
                
                };
        
    }
        [self editCarState:dic withTag:tag];
 
    
    
}
//修改车辆状态
-(void)editCarState:(NSDictionary *)params withTag:(NSInteger)tag{
    NSString *url;

    if (tag == 6) {
        url = [NSString stringWithFormat:@"%@%@",Api,carRevocationStockApi];
    }else{
       url = [NSString stringWithFormat:@"%@%@",Api,editCarStateApi];
    }
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:params progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        if ([response[@"code"] isEqual:@1]) {
            if (tag == 6) {
                [ShowMessage showHintMessage:@"撤销成功!" duration:2.0 withRemindId:remindSuccess];
                NSNotification *notificatiom = [NSNotification notificationWithName:@"FactorySuccess" object:self.indexPath userInfo:nil];
                [[NSNotificationCenter defaultCenter] postNotification:notificatiom];

            }else if (tag == 7){
                [ShowMessage showHintMessage:@"出库成功!" duration:2.0 withRemindId:remindSuccess];
                NSNotification *notificatiom = [NSNotification notificationWithName:@"FactorySuccess" object:self.indexPath userInfo:nil];
                [[NSNotificationCenter defaultCenter] postNotification:notificatiom];
            }else if (tag == 1){
                [ShowMessage showHintMessage:@"申请下架成功!" duration:2.0 withRemindId:remindSuccess];
                NSNotification *notificatiom = [NSNotification notificationWithName:@"FactorySuccess" object:self.indexPath userInfo:@{@"type":@"carSaleState",@"value":@30}];
                [[NSNotificationCenter defaultCenter] postNotification:notificatiom];
            }else if (tag == 3){
                [ShowMessage showHintMessage:@"申请上架成功!" duration:2.0 withRemindId:remindSuccess];
                NSNotification *notificatiom = [NSNotification notificationWithName:@"FactorySuccess" object:self.indexPath userInfo:@{@"type":@"carSaleState",@"value":@20}];
                [[NSNotificationCenter defaultCenter] postNotification:notificatiom];
            }else if (tag == 4){
                [ShowMessage showHintMessage:@"申请复检成功!" duration:2.0 withRemindId:remindSuccess];
                NSNotification *notificatiom = [NSNotification notificationWithName:@"FactorySuccess" object:self.indexPath userInfo:@{@"type":@"carDetectState",@"value":@530}];
                [[NSNotificationCenter defaultCenter] postNotification:notificatiom];
            }else if (tag == 5){
                [ShowMessage showHintMessage:@"取消预订成功!" duration:2.0 withRemindId:remindSuccess];
                NSNotification *notificatiom = [NSNotification notificationWithName:@"FactorySuccess" object:self.indexPath userInfo:@{@"type":@"carOrderState",@"value":@0}];
                [[NSNotificationCenter defaultCenter] postNotification:notificatiom];
            }
            self.callback(@{@"success":@(1)});
        }else{
            [ShowMessage showHintMessage:response[@"msg"] duration:2.0 withRemindId:remindFail];

        }
        
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];

    }];
    
}



@end
